﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net.Nntp
{
    using System;
    using System.IO;

    /// <summary>Is the base class of all responses that contain the identifiers (article number and message-id) of a
    /// single article as well as a part or the whole of the article content.</summary>
    /// <typeparam name="TMostDerived">The most-derived subclass of this type.</typeparam>
    /// <threadsafety static="true" instance="false"/>
    public abstract class ArticleContentResponse<TMostDerived> : ArticleIdResponse<TMostDerived>, IDisposable
        where TMostDerived : ArticleContentResponse<TMostDerived>
    {
        private readonly Stream content;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Gets the article content.</summary>
        /// <value>A <see cref="Stream"/> object containing the whole article or a part thereof (depends on the
        /// deriving class).</value>
        public Stream Content
        {
            get { return this.content; }
        }

        /// <summary>See <see cref="IDisposable.Dispose"/>.</summary>
        /// <remarks>Disposes the <see cref="Stream"/> object returned by <see cref="Content"/>.</remarks>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal ArticleContentResponse(long number, string messageId, Stream content) : base(number, messageId)
        {
            this.content = content;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.content.Dispose();
            }
        }
    }
}
